/*
 * Filename:    UploadFileMsgHandle.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-1-21
 */
package com.paic.mhis.hcpms.common.uploadFile.biz.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import com.paic.mhis.common.cache.biz.service.BigShareCache;
import com.paic.mhis.common.redis.biz.service.RedisClientCallback;
import com.paic.mhis.common.redis.biz.service.RedisService;
import com.paic.pafa.app.lwc.core.util.FileCopyUtils;

@Component("uploadFileMsgHandle")
public class UploadFileMsgHandle extends JedisPubSub {

	private Log logger = LogFactory.getLog(this.getClass());
	@Value("${hcpmsDownloadPath}")
	private String filePath;
	@Autowired
	private RedisService redisService;
	@Resource
	private BigShareCache redisShareCache;

	@Override
	public void onMessage(String channel, String message) {
		logger.info("Here to process massage");
		logger.info(message);

		final String key = message;

		String fileName = redisService.access(new RedisClientCallback<String>() {

			@Override
			public String exec(Jedis jedis) {
				String filename = jedis.hget(key, "filename");
				return filename;
			}
		});

		String path = redisService.access(new RedisClientCallback<String>() {

			@Override
			public String exec(Jedis jedis) {
				String filename = jedis.hget(key, "path");
				return filename;
			}
		});
		byte[] filedata = redisService.access(new RedisClientCallback<byte[]>() {

			@Override
			public byte[] exec(Jedis jedis) {
				byte[] data = jedis.hget(key.getBytes(), "filedata".getBytes());
				return data;
			}
		});

		try {
			// 文件上传路径
			String fPath = filePath + File.separator + path;
			logger.info("--------------" + fileName + "----" + fPath + "-----------------------------");
			// 如果目录不存在，创建它
			File dirPath = new File(fPath);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
			final File uploadFile = new File(fPath, fileName);
			// byte[] k = fileContent;
			FileCopyUtils.copy(filedata, uploadFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onSubscribe(String channel, int subscribedChannels) {
		logger.info("to be implemented");
	}

	@Override
	public void onUnsubscribe(String channel, int subscribedChannels) {
		logger.info("to be implemented");
	}

	@Override
	public void onPMessage(String pattern, String channel, String message) {
		logger.info("to be implemented");
	}

	@Override
	public void onPSubscribe(String channel, int subscribedChannels) {
		logger.info("to be implemented");
	}

	@Override
	public void onPUnsubscribe(String channel, int subscribedChannels) {
		logger.info("to be implemented");
	}

}
